package com.itcast.donkey.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itcast.donkey.R;
import com.itcast.donkey.adapters.AttackAdapter;
import com.itcast.donkey.modles.AttackModles;
import com.itcast.donkey.modles.UriModles;
import com.yalantis.phoenix.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/23.
 */
public class AttackFragment extends Fragment {
    private RecyclerView recyclerView;
    private AttackModles asiaModles;
    private AttackModles europeModles;
    private AttackModles americeModles;
    private AttackModles hongkongModles;
    private AttackModles continentModles;
    private AttackAdapter adapter;

    private PullToRefreshView pull;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_attack, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        initData();
        initEvent();
    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_attack);
        recyclerView.setFocusable(true);
        pull = (PullToRefreshView) view.findViewById(R.id.pull_fragment_attack);
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                final Request request = new Request.Builder()
                        .url(UriModles.ATTACK_URL)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //获取网络失败

                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        try {
                            JSONArray jsonArray = new JSONArray(result);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                if (i == 0) {
                                    asiaModles = gson.fromJson(jsonArray.get(i).toString(), AttackModles.class);
                                }
                                if (i == 1) {
                                    europeModles = gson.fromJson(jsonArray.get(i).toString(), AttackModles.class);
                                }
                                if (i == 2) {
                                    americeModles = gson.fromJson(jsonArray.get(i).toString(), AttackModles.class);
                                }
                                if (i == 3) {
                                    hongkongModles = gson.fromJson(jsonArray.get(i).toString(), AttackModles.class);
                                }
                                if (i == 4) {
                                    continentModles = gson.fromJson(jsonArray.get(i).toString(), AttackModles.class);

                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (asiaModles.getDestinations().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "获取网络失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    LinearLayoutManager lineManager = new LinearLayoutManager(getActivity());
                    recyclerView.setLayoutManager(lineManager);

                    adapter = new AttackAdapter(getActivity(), asiaModles, europeModles, americeModles, hongkongModles, continentModles);
                    recyclerView.setAdapter(adapter);
                    break;
            }
        }
    };

    private void initEvent() {
        pull.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pull.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pull.setRefreshing(false);
                    }
                }, 3000);
            }
        });


    }

}
